package com.cx.base.day19_thread;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/12
 */
public class Thread_10_可见性 {
    private static boolean initFlag = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {

            System.out.println("waitting data...");
            while (!initFlag) {
            }
            System.out.println("success");
        }).start();
//        确保上面线程首先执行
        Thread.sleep(2000);
//        下面线程会在sleep了2s后修改initFlag,此时上面的线程依然在执行第12的while循环，说明上面线程未感知到initFlag的变化
        new Thread(() -> {
            System.out.println("change initFlag true ");
            initFlag = true;
            System.out.println("initFlag: " + initFlag);
        }).start();
    }
}
